# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import re
import pymysql

class MoviefetchPipeline(object):
    def __init__(self):
        pass        

    def process_item(self, item, spider):
        # self.file.write(item["url"])

        connection = pymysql.connect(host='rm-uf63864knb23927hlio.mysql.rds.aliyuncs.com', user='beibei' ,passwd='Beibei0312', db='helloworld', charset='utf8', port=3306)
        cue = connection.cursor()

        # print(item["code"],item["url"],item["title"])
        
        item_id = item["code"]

        if len(item["title"]) > 0 and len(item["url"]) > 0 :
            # print("mysql connect succes")#测试语句，这在程序执行时非常有效的理解程序是否执行到这一步
            try:  
                cue.execute("insert into movie(code ,title , url) values(%s,%s,%s) on DUPLICATE KEY UPDATE title=VALUES(title),url=VALUES(url)",[item_id,item['title'],item['url']])
                # print("insert success")#测试语句
            except Exception as e:  
                print('Insert error:',e)  
                connection.rollback()  
            else:  
                connection.commit()
            connection.close()

        return item
